﻿#include "dialog.h"
#include "xxl.h"
#include "ui_dialog.h"

#include <QDebug>
#include <QRandomGenerator>
#include <QMessageBox>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->toolButton->installEventFilter(this);
    OnConnect();
}

Dialog::~Dialog()
{
    delete ui;
}

bool Dialog::eventFilter(QObject *obj, QEvent *e)
{
    if(obj == ui->toolButton)
    {
        if(e->type() == QEvent::Enter)
        {
            double w,h;
            while(true)
            {
                w = QRandomGenerator::global()->bounded(0.99);
                h = QRandomGenerator::global()->bounded(0.99);
                w = this->width()*w;
                h = this->height()*h;
                if(w < this->width()-ui->toolButton->width() && h<this->height()-ui->toolButton->height())
                   break;
            }
            ui->toolButton->move(w,h);
           int pp = QRandomGenerator::global()->bounded(2,4);
            QString url = QString(":/res/%1.png").arg(pp);
            ui->toolButton->setIcon(QIcon(url));
        }
    }

    return QWidget::eventFilter(obj,e);
}

void Dialog::OnConnect()
{
    connect(ui->toolButton,&QToolButton::clicked,this,&Dialog::onButtonClickde);

}

void Dialog::xxlexec()
{
    xxl* x = new xxl(this);
    connect(x,&xxl::restart,this,&Dialog::xxlexec);
    x->exec();
}

void Dialog::onButtonClickde()
{
    QMessageBox::information(this,"","哎呀，还是被你抓到了,奖励一小游戏!");
    this->close();
    xxlexec();
}

